<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\utils\ErrorCode;
use think\facade\Cache;
use app\admin\utils\User as AdminUser;

class User extends Base
{
    public function login()
    {
        $data = request()->only(['username', 'password']);
        $username = $data['username'];
        $password = $data['password'];
        $token = AdminUser::login($username, $password);
        if ($token) {
            $info = ['token'=> $token];
            return $this->ok($info);
        }
        return $this->fail(ErrorCode::USRRNAME_PASSWORD_NOT_MATCH);
    }

    public function info()
    {
        $uid = AdminUser::getUid();
        $userInfo = model('Admin')->getUserInfo($uid);
        $data = [
            'name' => $userInfo['user_name'],
            'roles' => $userInfo['is_super'] ? ['super_admin'] : $userInfo['auth'],
            'avatar' => request()->domain(). '/static/img/default_face.jpg',
        ];
        return $this->ok($data);
    }

    public function logout()
    {
        AdminUser::removeToken();
        return $this->ok('退出成功');
    }
}
